.TH std::weak_ptr::expired 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::weak_ptr::expired \- std::weak_ptr::expired

.SH Synopsis
   bool expired() const noexcept;  \fI(since C++11)\fP

   Equivalent to use_count() == 0. The destructor for the managed object may not yet
   have been called, but this object's destruction is imminent (or may have already
   happened).

.SH Parameters

   \fI(none)\fP

.SH Return value

   true if the managed object has already been deleted, false otherwise.

.SH Notes

   This function is inherently racy if the managed object is shared among threads. In
   particular, a false result may become stale before it can be used. A true result is
   reliable.

.SH Example

   Demonstrates how expired is used to check validity of the pointer.


// Run this code

 #include <iostream>
 #include <memory>

 std::weak_ptr<int> gw;

 void f()
 {
     if (!gw.expired())
         std::cout << "gw is valid\\n";
     else
         std::cout << "gw is expired\\n";
 }

 int main()
 {
     {
         auto sp = std::make_shared<int>(42);
         gw = sp;

         f();
     }

     f();
 }

.SH Output:

 gw is valid
 gw is expired

.SH See also

   lock      creates a shared_ptr that manages the referenced object
             \fI(public member function)\fP
   use_count returns the number of shared_ptr objects that manage the object
             \fI(public member function)\fP
